//
//  BLCommendHeaderTopView.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/5/27.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit
import SDWebImage

class BLCommendHeaderTopView: UIView {
    
    let BLPictureSliderCellID = "BLPictureSliderCellID"
    
    lazy var collectionView  = UICollectionView()
    /// 定时器
    var timer : NSTimer?
    
    /// 数据数组
    lazy var slideArray = [BLSlideItem]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configureUI()
        getcyclePictureData()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 配置UI
extension BLCommendHeaderTopView{
    //设置UI
    func configureUI() {
        collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: BLCollectionViewFlowLayout())
        collectionView.backgroundColor = UIColor.clearColor()
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.bounces = false
        self.addSubview(collectionView)
        collectionView.registerNib(UINib.init(nibName: String(BLPictureSliderCell), bundle: nil), forCellWithReuseIdentifier: BLPictureSliderCellID)
    }
    
}

// MARK: - 设置collectionView代理与数据源
extension BLCommendHeaderTopView : UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return slideArray.count * 20000
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(BLPictureSliderCellID, forIndexPath: indexPath) as! BLPictureSliderCell
        cell.modal = slideArray[indexPath.item % slideArray.count]
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let modal = slideArray[indexPath.item % slideArray.count]
        NSNotificationCenter.defaultCenter().postNotificationName(kPushLiveRoomNotification, object: modal.roomDetal.room_id)
    }
    
}

// MARK: - scroll - Delegate
extension BLCommendHeaderTopView{
    
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        removeTimer()
    }
    
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        beginTimer()
    }
    
}

// MARK: - UI显示事件
extension BLCommendHeaderTopView{
    
    /// 开启定时器
    func beginTimer() {
        timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(pictureSlide), userInfo: nil, repeats: true)
        
        NSRunLoop.mainRunLoop().addTimer(timer!, forMode: NSRunLoopCommonModes)
        
    }
    /// 移除定时器
    func removeTimer() {
        timer?.invalidate()
    }
    /// 开始轮播事件
    func pictureSlide() {
        
        let indexPath = collectionView.indexPathsForVisibleItems().last
        let nextIndexPath = NSIndexPath(forItem: (indexPath?.item)! + 1, inSection: (indexPath?.section)!)
        
        collectionView.scrollToItemAtIndexPath(nextIndexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
    }
}

// MARK:- 网络请求
extension BLCommendHeaderTopView{
    //获取轮播器数据
    func getcyclePictureData(){
        
        BLGetDouyuData.shareInstance.getSlideData { (dataArray) in
            self.slideArray = dataArray
            //刷新表格
            self.collectionView.reloadData()
            self.beginTimer()
        }
    }
}